package com.lzf.yh.dingshiutil;

import com.lzf.yh.commons.DateUtil;
import com.lzf.yh.dao.GuitarInfoMapper;
import com.lzf.yh.dao.OrderInfoMapper;
import com.lzf.yh.dao.Recommend_guitarMapper;
import com.lzf.yh.entity.GuitarInfo;
import com.lzf.yh.entity.OrderInfo;
import com.lzf.yh.entity.Recommend_guitar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

/**
 * @Auther: Wyz
 * @Date: 2021/4/17 15:54
 * @Description:
 */
@Configuration      //标记配置类
@EnableScheduling   //开启定时任务
public class DingShiUtil {

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Autowired
    private GuitarInfoMapper guitarInfoMapper;

    @Autowired
    private Recommend_guitarMapper recommend_guitarMapper;

    @Scheduled(cron="0 * * * * ?")
    private void updateSecKillShopStatus() {
        String nowDate= DateUtil.convert2String(new Date(), "yyyy-MM-dd HH:mm:ss");
        System.err.println("执行静态定时任务时间: " + nowDate);
        List<OrderInfo> orderInfos = orderInfoMapper.selectAllOrderInfo();
        for (int i = 0; i < orderInfos.size(); i++) {
            String dealtime = orderInfos.get(i).getDealtime();
            Date date = DateUtil.convert2Date(dealtime, "yyyy-MM-dd HH:mm:ss");
            Date date1 = new Date(date.getTime() + 30 * 60 * 1000);
            String date30 = DateUtil.convert2String(date1, "yyyy-MM-dd HH:mm:ss");
            System.err.println("第"+i+"30分钟后的时间: " + date30);
            if (nowDate.equals(date30)){
                System.err.println("进来了");
                //获得到吉他名称
                String guitarname = orderInfos.get(i).getGuitarname();
                //获得购买的数量
                Integer count = orderInfos.get(i).getCount();
                //根据名字查询普通表,把普通表回滚
                GuitarInfo byName = guitarInfoMapper.selectByName(guitarname);
                GuitarInfo guitarInfo = new GuitarInfo();
                guitarInfo.setName(guitarname);
                guitarInfo.setCount(byName.getCount()+count);
                int i1 = guitarInfoMapper.updateCountByName(guitarInfo);
                //根据名字查询推荐表，如果存在也回滚
                int i2 = 1 ;
                Recommend_guitar recommend_guitar = recommend_guitarMapper.selectByName(guitarname);
                if (recommend_guitar!=null){
                    Recommend_guitar recommendGuitar = new Recommend_guitar();
                    recommendGuitar.setName(guitarname);
                    recommendGuitar.setCount(byName.getCount()+count);
                    i2 = recommend_guitarMapper.updateCountByName(recommendGuitar);
                }
                //回滚成功后把状态订单状态改为4（取消订单）
                if (i1>0&&i2>0){
                    orderInfoMapper.updateStartsById(orderInfos.get(i).getId());
                }
            }
        }
    }
}
